/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.book;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.book.BookOrder;
import com.boxin.ims.modules.wechat.entity.dic.Storefront;
import com.boxin.ims.modules.wechat.service.book.BookOrderService;
import com.boxin.ims.modules.wechat.service.dic.StorefrontService;

/**
 * 预定服务Controller
 * @author Jakemanse
 * @version 2013-12-08
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/book/bookOrder")
public class BookOrderController extends BaseController {

	@Autowired
	private BookOrderService bookOrderService;
	
	@Autowired
	private StorefrontService storefrontService;
	
	@ModelAttribute
	public BookOrder get(@RequestParam(required=false) Long id) {
		if (id != null){
			return bookOrderService.get(id);
		}else{
			return new BookOrder();
		}
	}
	
	@RequiresPermissions("wechat:book:bookOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(BookOrder bookOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			bookOrder.setUser(user);
		}
        Page<BookOrder> page = bookOrderService.find(new Page<BookOrder>(request, response), bookOrder); 
        model.addAttribute("page", page);
		return "modules/wechat/book/bookOrderList";
	}

	@RequiresPermissions("wechat:book:bookOrder:view")
	@RequestMapping(value = "form")
	public String form(BookOrder bookOrder, Model model) {
	
		User user = UserUtils.getUser();
		if (bookOrder.getUser() == null || bookOrder.getUser().getId() <1){
			bookOrder.setUser(user);
		}
		
		List<Storefront> storefrontList = storefrontService.findByXY(user.getId(), null);
		
		
		model.addAttribute("storefrontList", storefrontList);
		model.addAttribute("bookOrder", bookOrder);
		return "modules/wechat/book/bookOrderForm";
	}

	@RequiresPermissions("wechat:book:bookOrder:edit")
	@RequestMapping(value = "save")
	public String save(BookOrder bookOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bookOrder)){
			return form(bookOrder, model);
		}
		bookOrderService.save(bookOrder);
		addMessage(redirectAttributes, "保存预定服务'" + bookOrder.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/book/bookOrder/?repage";
	}
	
	@RequiresPermissions("wechat:book:bookOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		bookOrderService.delete(id);
		addMessage(redirectAttributes, "删除预定服务成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/book/bookOrder/?repage";
	}

}
